package com.wise.site.detail;

/**
 * 资讯详情
 */
import com.wise.site.R;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.InfoItem;
import com.wise.site.protocol.result.LoginResult;
import com.wise.site.utils.CollectorUtils;
import com.wise.site.utils.CollectorUtils.UploadData;
import com.wise.site.utils.CommentUtils;
import com.wise.site.utils.CommentUtils.CommentUploadData;
import com.wise.site.utils.ImageLoader.Imager;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.DataCache;
import com.wise.site.utils.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class InfoDetailsActivity extends Activity implements View.OnClickListener {
	private InfoItem mItem;
	private TextView commentText;
	private View shareBnt;
	private View collectBnt;
	private View submitBnt;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_details_activity);
		findViewById(R.id.title_back).setOnClickListener(this);
		findViewById(R.id.activity_title_comment).setOnClickListener(this);
		//findViewById(R.id.info_comment).setOnFocusChangeListener(this);
		
		
		shareBnt = findViewById(R.id.share_img);
		shareBnt.setOnClickListener(this);
		collectBnt = findViewById(R.id.collect_img);
		collectBnt.setOnClickListener(this);
		submitBnt = findViewById(R.id.sub_comment_bnt);
		submitBnt.setOnClickListener(this);
		commentText = ((TextView)findViewById(R.id.info_comment));
		commentText.setOnClickListener(this);
		//((TextView)findViewById(R.id.activity_title)).setText(getString(R.string.detail));
		Intent intent = getIntent();
		mItem = (InfoItem) intent.getSerializableExtra(Constanct.INFO_ENTRY);
		
		bindData(mItem);
	}
	
	private void bindData(InfoItem item){
		if(item.iconURL != null && item.iconURL.length() > 5){
			ImageView icon = (ImageView)findViewById(R.id.info_icon);
			icon.setVisibility(View.VISIBLE);
			ImageLoader.getInstance().DisplayImage( new Imager(item.iconURL, icon));
		}
		((TextView)findViewById(R.id.info_title)).setText(item.tilte);
		((TextView)findViewById(R.id.info_sub_title)).setText(getString(R.string.info_detail_subtitle, item.date, item.source));
		((TextView)findViewById(R.id.info_content)).setText(Html.fromHtml(item.content));
	}
	
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
			shareBnt.setVisibility(View.VISIBLE);
			collectBnt.setVisibility(View.VISIBLE);
			submitBnt.setVisibility(View.GONE);
		}
		return super.dispatchKeyEvent(event);
	}
	
	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		int id = v.getId();
		switch(id){
		case R.id.info_comment:
			if(DataCache.getInstance().get(Constanct.ISLOGINED_KEY) == null){
				sendBroadcast(new Intent("sw.login"));
				finish();
				break;
			}
			shareBnt.setVisibility(View.GONE);
			collectBnt.setVisibility(View.GONE);
			submitBnt.setVisibility(View.VISIBLE);
			break;
		case R.id.sub_comment_bnt:
			LoginResult lr = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
			if(lr == null){
				sendBroadcast(new Intent("sw.login"));
				finish();
				break;
			}
			CommentUtils.getInstance().uploadComment(new CommentUploadData(mItem.id, lr.id, lr.userName, commentText.getText().toString()), 3, null, this);
			shareBnt.setVisibility(View.VISIBLE);
			collectBnt.setVisibility(View.VISIBLE);
			submitBnt.setVisibility(View.GONE);
			commentText.setText("");
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
			break;
		case R.id.title_back:
			finish();
			break;
		case R.id.share_img:
			intent.setClass(this, DetailShareActivity.class);
			startActivity(intent);
			break;
		case R.id.collect_img:
			if(mItem != null){
				LoginResult lr1 = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
				if(lr1 == null){
					sendBroadcast(new Intent("sw.login"));
					finish();
					break;
				}
				
				CollectorUtils.getInstance().uploadData(new UploadData(mItem.id, 3, lr1.id, 0), this);
			}
			break;
		case R.id.activity_title_comment:
			intent.setClass(this, DetailCommentActivity.class);
			intent.putExtra(Constanct.INFO_ENTRY, mItem.id);
			intent.putExtra(Constanct.INFO_TYPE, 3);
			intent.putExtra(Constanct.INTENT_TITLE, mItem.tilte);
			intent.putExtra(Constanct.INTENT_SUB_TITLE, getString(R.string.info_detail_subtitle, mItem.date, mItem.source));
			startActivity(intent);
			break;
		}
	}
	
	/*@Override
	public void onFocusChange(View v, boolean hasFocus) {
		int visibility = hasFocus ? View.GONE : View.VISIBLE;
		findViewById(R.id.collect_img).setVisibility(visibility);
		findViewById(R.id.share_img).setVisibility(visibility);
		visibility = !hasFocus ? View.GONE : View.VISIBLE;
		findViewById(R.id.send_img).setVisibility(visibility);
	}*/
	
	
	/*@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
			if(event.getAction() == KeyEvent.ACTION_UP){
				String msg = ((EditText)v).getText().toString();
				if(! TextUtils.isEmpty(msg)){
					CommentUtils.CommentUploadData data = new CommentUtils.CommentUploadData();
					//CommentUtils.getInstance().uploadComment(data, type, listener);
					((EditText)v).setText(null);
					Toast.makeText(this, R.string.comment_success, Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(this, R.string.comment_content_null_msg, Toast.LENGTH_SHORT).show();
				}
			}
			return true;
		}
		return false;
	}*/
}
